﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace BTSDecompress
{
    public partial class NameSpacesDialog : Form
    {
        #region Variables
        System.Collections.Hashtable _namespaces = new System.Collections.Hashtable();
        #endregion
        #region Constructors
        public NameSpacesDialog()
        {
            InitializeComponent();
        }
        public NameSpacesDialog(System.Collections.Hashtable namespaces)
        {
            InitializeComponent();
            this.Namespaces = namespaces;
            Initialize();
        }
        public NameSpacesDialog(ListView.ListViewItemCollection namespaces)
        {
            InitializeComponent();

            foreach (ListViewItem item in namespaces)
            {
                string[] s = item.Text.Split(' ');
                Namespaces.Add(s[0].Substring(0, s[0].Length - 1), s[1]);
            }

            //this.Namespaces = Namespaces;
            Initialize();
        }
        #endregion
        #region Properties
        public System.Collections.Hashtable Namespaces
        {
            get { return _namespaces; }
            set { _namespaces = value; }
        }
	    #endregion        
        #region Methods
        private void AddItemToListView()
        {

            if(PrefixExists(prefixTextBox.Text))
            {
                MessageBox.Show("Prefix already exists in the list.\r\nChoose a unique prefix.", "BTSDecompress", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            string[] row1 = { namespaceTextBox.Text.ToString() };
            nameSpacesListView.Items.Add(prefixTextBox.Text.ToString()).SubItems.AddRange(row1);

            prefixTextBox.Text = "";
            namespaceTextBox.Text = "";
        }
        private bool PrefixExists(string prefix)
        {
            foreach (ListViewItem item in nameSpacesListView.Items)
            {
                if (item.Text == prefix)
                {
                    return true;
                }
            }
            return false;
        }
        private void DeleteNamespace()
        {
            if (nameSpacesListView.SelectedItems.Count > 0)
            {
                DialogResult dr = MessageBox.Show("Delete this namespace?", "BTSDecompress", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

                if (dr == DialogResult.Yes)
                {
                    nameSpacesListView.Items[nameSpacesListView.SelectedIndices[0]].Remove();
                }
            }
        }
        private void EnableDisableAddButton()
        {
            if (prefixTextBox.Text.Length > 0 && namespaceTextBox.Text.Length > 0 && nameSpacesListView.SelectedIndices.Count==0)
            {
                addButton.Enabled = true;
            }
            else
            {
                addButton.Enabled = false;
            }
        }
        private void EnableDisableDeleteButton()
        {
            deleteButton.Enabled = (nameSpacesListView.SelectedItems.Count > 0);
        }
        private void EnableDisableEditButton()
        {
            if (prefixTextBox.Text.Length == 0 && namespaceTextBox.Text.Length == 0 && nameSpacesListView.SelectedItems.Count > 0)
            {
                editButton.Enabled = true;
                ListViewItem item = nameSpacesListView.SelectedItems[0];

                prefixTextBox.Text = item.Text;
                namespaceTextBox.Text = item.SubItems[1].Text.ToString();
            }
            else
            {
                editButton.Enabled = false;
                prefixTextBox.Text = "";
                namespaceTextBox.Text = "";
            }
        }
        private void FillNameSpacesListBox()
        {
            // Fill the ListView with the namespaces
            foreach (System.Collections.DictionaryEntry entry in _namespaces)
            {
                string[] row1 = { entry.Value.ToString() };
                nameSpacesListView.Items.Add(entry.Key.ToString()).SubItems.AddRange(row1);
            }
        }
        private void Initialize()
        {
            FillNameSpacesListBox();
        }
        private void SaveNamespace()
        {
            editButton.Enabled = true;
            ListViewItem item = nameSpacesListView.SelectedItems[0];

            item.Text = prefixTextBox.Text;
            item.SubItems[1].Text = namespaceTextBox.Text;
        }
        private void SaveNamespaces()
        {
            // Clear the current namespaces
            _namespaces.Clear();

            // Fill with the namespaces from the ListView
            foreach (ListViewItem item in nameSpacesListView.Items)
            {
                _namespaces.Add(item.Text, item.SubItems[1].Text);
            }
        }
        #endregion
        #region Events
        private void addButton_Click(object sender, EventArgs e)
        {
            AddItemToListView();
        }
        private void editButton_Click(object sender, EventArgs e)
        {
            SaveNamespace();
        }
        private void deleteButton_Click(object sender, EventArgs e)
        {
            DeleteNamespace();
        }
        private void nameSpacesListView_SelectedIndexChanged(object sender, EventArgs e)
        {
            EnableDisableEditButton();
            EnableDisableDeleteButton();
        }
        private void namespaceTextBox_TextChanged(object sender, EventArgs e)
        {
            EnableDisableAddButton();
        }
        private void okButton_Click(object sender, EventArgs e)
        {
            SaveNamespaces();
        }
        private void prefixTextBox_TextChanged(object sender, EventArgs e)
        {
            EnableDisableAddButton();
        }
        #endregion
    }
}
